﻿#include "mytablewidget.h"
#include <QPoint>
#include <QMenu>
#include <QMessageBox>
#include <QHeaderView>
#include <QApplication>
#include <QIcon>
#include <QColor>
#include <QDateTime>
#include <QPalette>
#include <QComboBox>
#include <QStringList>
#include <QItemDelegate>
#include <QSize>
#include <QItemSelectionModel>
#include "mytableitemdelegate.h"
#include "SetColValDlg.h"
#include <QTableWidgetSelectionRange>
#include <QList>
#include <QScrollBar>
#include <QAbstractButton>
#include <QPalette>
#include <QClipboard>
#include <QMimeData>
#include <QUrl>
#include "SaveDevToModelDlg.h"
#include "SaveDevToModelFile.h"
//#include "SaveTerminalToModelDlg.h"

MyTableWidget::MyTableWidget(MyTableData *pMyTableData, MyTreeWidget *pTreeWdgt, QWidget *parent) : QTableWidget(parent)
{
	m_CurSeltRowNo = -1;
	m_CurSeltColumnNo = -1;
	m_pTableData = NULL;
	m_SetColorRecId1=0;
	m_SetColorRecId2=0;
	m_SetColorRecId3=0;
	m_SetColorRecFieldNo=0;

	m_pHorizontalScrollBar = NULL;
	m_pHorizontalScrollBar = horizontalScrollBar();
	m_pCurTreeItem = NULL;
	m_RighMenu = NULL;
	m_bModified = false;
	m_LastSelRowNum = 0;
	m_LastSelItemRowNum = 0;
	m_LastSelItemColNum = 0;
	m_LastbSetColFlag = false;
	m_pTreeWdgt = pTreeWdgt;
	m_pTableData = pMyTableData;
	QColor	   tempcolor;
	QPalette   oldPalette = this->palette();
	tempcolor = oldPalette.color(QPalette::Highlight);
	oldPalette.setColor(QPalette::Highlight, tempcolor);
	oldPalette.setColor(QPalette::HighlightedText, Qt::white);
	setPalette(oldPalette);
	if (verticalHeader())
	{
		connect(verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(OnClickVHead(int)));		
	}

	MyTableItemDelegate *pdelegate = new MyTableItemDelegate(m_pTableData,/*PtlParamType,*/ this);             //创建一个自定义的代理类
	if (pdelegate)
	{
		setItemDelegate(pdelegate);
		connect(pdelegate, SIGNAL(RefreshShow(int, int, int,int)), this, SLOT(OnReSetDataShow(int, int, int,int)));	
		connect(pdelegate, SIGNAL(UserDataChang(int, int)), this, SLOT(OnItemUserDataChang(int, int)));
	}
	connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(SelectChanged()));
	connect(this, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(OnItemClick(QTableWidgetItem*)));
	setupPopMenu();
	//setHorizontalScrollMode(ScrollPerPixel);
	//ccg17.04.25
	//setSortingEnabled(false);

}

MyTableWidget::~MyTableWidget()
{
	m_pTableData->ReleaseTableData();
}

void MyTableWidget::ClearAllShowCont()
{
	while (rowCount()>0)
	{
		removeRow(0);
	}
	setColumnCount(0);
	m_pCurTreeItem = NULL;
	m_CurAppType=0;
	m_CurTableId=0;
	m_CurParentId=0;
	m_CurPtlParamType=0;
}

//	初始化当前选中树ITEM数据显示
int MyTableWidget::InitCurTreeItemDataShow(QTreeWidgetItem *pCurTreeItem,int PtlParamType)
{

	m_CurPtlParamType=PtlParamType;

	//jwj 2016-09-03
	/*
	if (PtlParamType>0)
	{
		if (pCurTreeItem)
		{
			TreeItemData CurItemData;
			CurItemData = pCurTreeItem->data(0, Qt::UserRole).value<TreeItemData>();
			m_CurParentId = CurItemData.parentId;
		}
		else
		{
			setColumnCount(0);
			setRowCount(0);
			return 0;
		}
		InitTableDataByTableType();
		if (!alternatingRowColors())
		{
			setAlternatingRowColors(true);
		}
	}
	else
		*/
	{
		if (pCurTreeItem)
		{
			//connect(this, SIGNAL(UpTreeItem_Signal(quint32, quint32, quint32, quint32,QTreeWidgetItem *, int)), m_pTreeWdgt, SLOT(OnUpDateTreeItem_byItem(quint32, quint32, quint32, quint32,QTreeWidgetItem *, int)));
			TreeItemData CurItemData;
			if (pCurTreeItem == m_pCurTreeItem)
			{
				CurItemData = pCurTreeItem->data(0, Qt::UserRole).value<TreeItemData>();
				if (CurItemData.AppType==SYSTEM_TYPE_NSA_YD && CurItemData.ShowTableId ==TABLE_RTASK_PARA_INFO && PtlParamType==0)
				{
					setColumnCount(0);
					setRowCount(0);
					return 0;
				}
				OnRefreshData();
			}else
			{
				m_pCurTreeItem = pCurTreeItem;
				CurItemData = pCurTreeItem->data(0, Qt::UserRole).value<TreeItemData>();
				
//jwj 2016-09-03
// 				if (CurItemData.AppType==SYSTEM_TYPE_NSA_YD && CurItemData.ShowTableId ==TABLE_RTASK_PARA_INFO && PtlParamType==0)
// 				{
// 					setColumnCount(0);
// 					setRowCount(0);
// 					return 0;
// 				}

				//	qDebug() << "new Apptype=" <<CurItemData.AppType <<"Tableid ="
				//		<<CurItemData.ShowTableId << "parentId"<< CurItemData.parentId <<"Recordid"<<CurItemData.RecordId;
				InitDataShow(CurItemData.AppType, CurItemData.ShowTableId, CurItemData.parentId,CurItemData.PtlParamType);
			}
		}
	}
	return 0;
}

int	MyTableWidget::InitTableDataByTableType()
{
	setColumnCount(2);
	QTableWidgetItem *add_item=new QTableWidgetItem;
	add_item->setText(QString::fromLocal8Bit("参数名称"));
	add_item->setIcon(QIcon(QString::fromLocal8Bit(":/Res/images/open.png")));
	add_item->setTextAlignment(Qt::AlignLeft);
	setHorizontalHeaderItem(0, add_item);
	add_item = new QTableWidgetItem;
	add_item->setText(QString::fromLocal8Bit("参数值"));
	add_item->setIcon(QIcon(QString::fromLocal8Bit(":/Res/images/save.png")));
	add_item->setTextAlignment(Qt::AlignLeft);
	setHorizontalHeaderItem(1, add_item);
	setColumnWidth(0,300);

	char *buff = NULL;
	int	buff_len = 0;
	int	TableRecordLen =0;
	int	RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO,m_CurParentId, buff, &buff_len, &TableRecordLen,true);

	if (TableRecordLen%sizeof(rTask_Para_Info)!=0 || TableRecordLen/sizeof(rTask_Para_Info)!=RecordNum)
	{
		setColumnCount(0);
		setRowCount(0);
		return -1;
	}

	m_ParaList.clear();

	int temp_data=0;
	QString temp_str="";
	int temp_row_count=0;
	switch (m_CurPtlParamType)
	{
	case ENUM_TASK_PARA_TYPE_101:
		{
			for (int i=0;i<SYSTEM_PARA_101_MAXCOUNT;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_101_STR[i]);
				if (temp_str=="null")
				{
					temp_str="";
					break;
				}
				temp_row_count++;
			}
			setRowCount(temp_row_count);
			for (int i=0;i<temp_row_count;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_101_STR[i]);
				add_item = new QTableWidgetItem;
				add_item->setText(temp_str);
				add_item->setFlags(add_item->flags()&~Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,0,add_item);
				
				temp_data=0;
				memcpy(&temp_data,buff+(i+2)*sizeof(int),sizeof(int));

				add_item = new QTableWidgetItem;
				add_item->setText(QString::number(temp_data));
				add_item->setFlags(add_item->flags() | Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,1,add_item);

				m_ParaList.append(QString::number(temp_data));
			}
		}
		break;
	case ENUM_TASK_PARA_TYPE_103:
		{
			for (int i=0;i<SYSTEM_PARA_103_MAXCOUNT;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_103_STR[i]);
				if (temp_str=="null")
				{
					temp_str="";
					break;
				}
				temp_row_count++;
			}
			setRowCount(temp_row_count);
			for (int i=0;i<temp_row_count;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_103_STR[i]);
				add_item = new QTableWidgetItem;
				add_item->setText(temp_str);
				add_item->setFlags(add_item->flags()&~Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,0,add_item);

				temp_data=0;
				memcpy(&temp_data,buff+(i+2)*sizeof(int),sizeof(int));

				add_item = new QTableWidgetItem;
				add_item->setText(QString::number(temp_data));
				add_item->setFlags(add_item->flags() | Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,1,add_item);

				m_ParaList.append(QString::number(temp_data));
			}
		}
		break;
	case ENUM_TASK_PARA_TYPE_104:
		{
			for (int i=0;i<SYSTEM_PARA_104_MAXCOUNT;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_104_STR[i]);
				if (temp_str=="null")
				{
					temp_str="";
					break;
				}
				temp_row_count++;
			}
			setRowCount(temp_row_count);
			for (int i=0;i<temp_row_count;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_104_STR[i]);
				add_item = new QTableWidgetItem;
				add_item->setText(temp_str);
				add_item->setFlags(add_item->flags()&~Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,0,add_item);

				temp_data=0;
				memcpy(&temp_data,buff+(i+2)*sizeof(int),sizeof(int));

				add_item = new QTableWidgetItem;
				add_item->setText(QString::number(temp_data));
				add_item->setFlags(add_item->flags() | Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,1,add_item);

				m_ParaList.append(QString::number(temp_data));
			}
		}
		break;
	case ENUM_TASK_PARA_TYPE_61850:
		{
			for (int i=0;i<SYSTEM_PARA_61850_MAXCOUNT;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_61850_STR[i]);
				if (temp_str=="null")
				{
					temp_str="";
					break;
				}
				temp_row_count++;
			}
			setRowCount(temp_row_count);
			for (int i=0;i<temp_row_count;i++)
			{
				temp_str=QString::fromLocal8Bit(SYSTEM_PARA_61850_STR[i]);
				add_item = new QTableWidgetItem;
				add_item->setText(temp_str);
				add_item->setFlags(add_item->flags()&~Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,0,add_item);

				temp_data=0;
				memcpy(&temp_data,buff+(i+2)*sizeof(int),sizeof(int));

				add_item = new QTableWidgetItem;
				add_item->setText(QString::number(temp_data));
				add_item->setFlags(add_item->flags() | Qt::ItemIsEditable);
				add_item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
				setItem(i,1,add_item);

				m_ParaList.append(QString::number(temp_data));
			}
		}
		break;
	default:
		{
			setColumnCount(0);
			setRowCount(0);
		}
		break;
	}
	return 1;
}

//初始化数据显示
int MyTableWidget::InitDataShow(unsigned int AppType, unsigned int TableId, unsigned int ParentId, int PtlParamType)
{
	if (AppType<=0 && TableId <=0)
	{
		return -1;
	}
	m_CurAppType = AppType;
	m_CurTableId = TableId;
	m_CurParentId = ParentId;
	m_CurPtlParamType = PtlParamType;
	if (InitTableParam(AppType, TableId, ParentId,PtlParamType) <= 0)
	{
		return -2;
	}

	if (InitTableHead() <= 0)
	{
		return -3;
	}

	if (InitTableDataShow(false, 0)<=0)
	{
		return -4;
	}
	CheckNeedSave(false);
	return 0;
}

//  刷新表数据显示（重新从数据内存中读取数据显示）
int MyTableWidget::RefrashTableData()
{
	if (!m_pTableData->IsRecordParamSafe())
	{
		return -1;
	}
	//jwj 2017-02-20
	m_pTableData->ReInitTableData();
	int PageNum =m_pTableData->GetPageNum();
	emit PageNum_Signal(PageNum);
	ShowTableData(true, m_pTableData->GetCurPageNo());

//	return InitTableDataShow(true, m_pTableData->GetCurPageNo());
}

//	初始化表表参数和数据
int MyTableWidget::InitTableParam(unsigned int AppType, unsigned int TableId, unsigned int ParentId, int PtlParamType)
{
	if (AppType<=0 && TableId <=0)
	{
		return -1;
	}
//	m_pTableData->InitTableParam(AppType, TableId, ParentId,PtlParamType);
	if (!m_pTableData->IsRecordParamSafe())
	{
		return -2;
	}
	int FieldNum = 0;
	FieldNum = m_pTableData->GetCurShowFieldNum();
	if (FieldNum <= 0)
	{
		return -3;
	}

	setColumnCount(FieldNum);

	setCornerButtonEnabled(false);
	verticalHeader()->setResizeMode(QHeaderView::Fixed);  
	if (!alternatingRowColors())
	{
		setAlternatingRowColors(true);
	}

	if (!IsCurTableEnableEdit(AppType, TableId, ParentId) )
	{
		setEditTriggers(QAbstractItemView::NoEditTriggers/*QAbstractItemView::AllEditTriggers*/);
	}

	return 1;
}

//	初始化表头显示
int MyTableWidget::InitTableHead()
{
	if (m_pTableData->IsRecordParamSafe())
	{
		QTableWidgetItem *HeadItem = NULL;
		QString HeadItemText;
		QString HeadIcon;
		int	j = 0;
		int	ColWidth = 0;
		int FieldNum = m_pTableData->GetCurShowFieldNum();

		for (j=0; j<FieldNum; j++)
		{
			m_pTableData->GetHeadText(j, HeadItemText, ColWidth);
			m_pTableData->GetHeadIcon(j, HeadIcon);

			HeadItem = new QTableWidgetItem;
			HeadItem->setText(HeadItemText);
			HeadItem->setIcon(QIcon(HeadIcon));
			HeadItem->setTextAlignment(Qt::AlignLeft);
			setHorizontalHeaderItem(j, HeadItem);
			if (ColWidth<=0)
			{
				ColWidth = 80;
			}
			setColumnWidth(j, ColWidth);
		}
		return 1;
	}
	return -1;
}

//	初始化表数据显示
int MyTableWidget::InitTableDataShow(bool bShowOldPage, int OldPageNo)
{
	if (!m_pTableData->IsRecordParamSafe())
	{
		return -1;
	}
	//jwj 2017-02-20
	m_pTableData->ReInitTableData();
	int PageNum =m_pTableData->GetPageNum();
	emit PageNum_Signal(PageNum);
	ShowTableData(bShowOldPage, OldPageNo);

	return 1;
}
int MyTableWidget::ShowTableData(bool bShowOldPage, int OldPageNo)
{
	if (!m_pTableData->IsRecordParamSafe())
	{
		return -1;
	}
	while (rowCount()>0)
	{
		removeRow(0);
	}
	int	 RecordNum = 0;
	int	 CurPageNo=0;
	if (bShowOldPage)
	{
		CurPageNo = OldPageNo;
		m_pTableData->SetCurPageNo(CurPageNo);
	}else
	{
		CurPageNo = m_pTableData->GetCurPageNo();
	}

	emit PageCurNo_Signal(CurPageNo);
	RecordNum = m_pTableData->GetCurPageRecordNum();
	setRowCount(RecordNum);
	if (RecordNum<=0)
	{
		return -2;
	}

	for (int i=0; i<RecordNum; i++)
	{
		OneRowTableDataShow(i);
	}
   	return  1;
}

int MyTableWidget::SetVerticalHeadShow(int RowNo, QTableWidgetItem *pVHeadItem, bool bDelFlag)
{
	if (pVHeadItem)
	{
		int CurPageNo = m_pTableData->GetCurPageNo();
		QString				ItemText;
		ItemText= QString("%1").arg(RowNo+1+CurPageNo*TABLE_PAGE_ROW_NUM);	
		pVHeadItem->setText(ItemText);
		pVHeadItem->setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
		pVHeadItem->setSizeHint(QSize(40, 20));
		if (bDelFlag)
		{
			pVHeadItem->setIcon(QIcon(QString::fromLocal8Bit(":Res/images/delete.png")));
		}else
		{
			pVHeadItem->setIcon(QIcon(QString::fromLocal8Bit("")));
		}
		pVHeadItem->setData(Qt::UserRole, QVariant((bool)bDelFlag));
		setVerticalHeaderItem(RowNo, pVHeadItem);		
		return 1;
	}
	return 0;
}

//	显示一条记录
int MyTableWidget::OneRowTableDataShow(int RowNo)
{
	if (RowNo>=0 && RowNo<=rowCount())
	{
		QTableWidgetItem	*pItem = NULL;
		QTableWidgetItem	*pVHeadItem = NULL;
		QString				ItemText;
		int				j = 0;
		int				FieldNum = 0;
		bool				bColorField = false;
		bool				bDevTable = false;
		QVariant			ItemVarData;
		bool				bChangeFlag = false;
		bool				bDeleteFlag = false;
		FieldNum = m_pTableData->GetCurShowFieldNum();
		if (FieldNum<=0)
		{
			return -1;
		}
		if (SYSTEM_TYPE_NSA_YD == m_CurAppType &&
			TABLE_RDEVS_INFO == m_CurTableId)
		{
			bDevTable = true;
		}

		pVHeadItem = verticalHeaderItem(RowNo);
		if (!pVHeadItem)
		{
			pVHeadItem = new QTableWidgetItem;
		}
		bDeleteFlag = m_pTableData->IsCurRecordSetDeleteFlag(RowNo);
		SetVerticalHeadShow(RowNo, pVHeadItem, bDeleteFlag);
		bool bSetColorFlag = false;
		if (m_SetColorRecId1>0||
			m_SetColorRecId2>0||
			m_SetColorRecId3>0)
		{
			unsigned int CurRec_Id = 0;
			m_pTableData->GetCurRecordIdbyRowIndex(RowNo, CurRec_Id);
			bSetColorFlag = CheckIsSetChangColorRecord(CurRec_Id);
		}
		unsigned int	ShowFlag = 0;
		bool bEdit = false;
		bool bEdit_Once = false;
		for (j=0; j<FieldNum; j++)
		{
			bChangeFlag = false;
// 			if (j>1)
// 			{
// 				m_pTableData->GetFieldText(RowNo, j, ItemVarData, ItemText, bColorField,bChangeFlag,item(RowNo,j-1)->data(Qt::UserRole));
// 			}
// 			else
// 			{
//				m_pTableData->GetFieldText(RowNo, j, ItemVarData, ItemText, bColorField,bChangeFlag,-1);
//			}
			m_pTableData->GetFieldText(RowNo, j, ItemVarData, ItemText, bColorField, ShowFlag);
			pItem = NULL;
			pItem = item(RowNo, j);
			if (!pItem)
			{
				pItem = new QTableWidgetItem;
				setItem(RowNo, j, pItem);
			}

			pItem->setText(ItemText);
			pItem->setData(Qt::ToolTipRole, ItemText);
			pItem->setData(Qt::UserRole, ItemVarData);
			pItem->setData(Qt::WhatsThisRole, ItemText);
			if (j==0)
			{				
				pItem->setBackgroundColor(QColor(205, 205, 205));
			}
			if ((ShowFlag&SHOW_FLAG_UNSAVE)>0)
			{
				pItem->setData(Qt::TextColorRole, Qt::green);
			}else if (bSetColorFlag && j == m_SetColorRecFieldNo)
			{
				pItem->setData(Qt::TextColorRole, Qt::magenta);
			}else 
			{
				pItem->setData(Qt::TextColorRole, Qt::black);
			}

			if (bColorField)
			{
				pItem->setData(Qt::DecorationRole,QColor(QRgb(ItemVarData.toUInt())));
			}
			m_pTableData->IsCurFieldEnableEdit(j, bEdit, bEdit_Once);
			if (!bEdit)
			{
				if (bEdit_Once && m_pTableData->IsCurRecordNewInsertFlag(RowNo))
				{
					pItem->setFlags(Qt::ItemIsSelectable | 
						Qt::ItemIsEditable | 
						Qt::ItemIsDragEnabled |
						Qt::ItemIsDropEnabled |
						Qt::ItemIsUserCheckable |
						Qt::ItemIsEnabled |
						Qt::ItemIsTristate);
				}else
				{
					pItem->setFlags(Qt::ItemIsSelectable |
						Qt::ItemIsDragEnabled |
						Qt::ItemIsEnabled |
						Qt::ItemIsTristate);
				}
			}else
			{
				if (bDeleteFlag)
				{
					pItem->setFlags(Qt::ItemIsSelectable | 
						Qt::ItemIsDragEnabled |
						Qt::ItemIsEnabled |
						Qt::ItemIsTristate);
				}else
				{
					pItem->setFlags(Qt::ItemIsSelectable | 
						Qt::ItemIsEditable | 
						Qt::ItemIsDragEnabled |
						Qt::ItemIsDropEnabled |
						Qt::ItemIsUserCheckable |
						Qt::ItemIsEnabled |
						Qt::ItemIsTristate);
				}
			}

		
		}

		return 1;
	}
	return 0;
}

//	显示一条字段（修改字段显示）
int MyTableWidget::OneFieldDataShow(int RowNo, int FieldNo)
{
	if (RowNo>=0 && RowNo < rowCount() && FieldNo>=0 && FieldNo<columnCount())
	{
		QTableWidgetItem	*pItem = NULL;
		QString				ItemText;
		bool				bColorField = false;
		bool				bEdit = false;
		bool				bEdit_Once = false;
		QVariant			ItemVarData;
		unsigned int		ShowFlag =0;
// 		if (FieldNo>1)
// 		{
// 			m_pTableData->GetUpDateFieldText(RowNo, FieldNo, ItemVarData, ItemText, bColorField,item(RowNo,FieldNo-1)->data(Qt::UserRole));
// 		}
// 		else
// 		{
//			m_pTableData->GetUpDateFieldText(RowNo, FieldNo, ItemVarData, ItemText, bColorField,-1);
//		}
		m_pTableData->GetFieldText(RowNo, FieldNo, ItemVarData, ItemText, bColorField,ShowFlag);

		pItem = NULL;
		pItem = item(RowNo, FieldNo);
		if (!pItem)
		{
			return 0;
		}
		pItem->setText(ItemText);
		pItem->setData(Qt::ToolTipRole, ItemText);
		pItem->setData(Qt::UserRole, ItemVarData);
		pItem->setData(Qt::TextColorRole, Qt::red);
		if (FieldNo==0)
		{
			pItem->setBackgroundColor(QColor(205, 205, 205));
		}
		if ( (ShowFlag&SHOW_FLAG_UNSAVE)>0 )
		{
			pItem->setData(Qt::TextColorRole, Qt::green);
		}
		if (bColorField)
		{
			pItem->setData(Qt::DecorationRole,QColor(QRgb(ItemVarData.toUInt())));
		}

		m_pTableData->IsCurFieldEnableEdit(FieldNo, bEdit, bEdit_Once);
		if (!bEdit)
		{
			if (bEdit_Once && m_pTableData->IsCurRecordNewInsertFlag(RowNo))
			{
				pItem->setFlags(Qt::ItemIsSelectable | 
					Qt::ItemIsEditable | 
					Qt::ItemIsDragEnabled |
					Qt::ItemIsDropEnabled |
					Qt::ItemIsUserCheckable |
					Qt::ItemIsEnabled |
					Qt::ItemIsTristate);
			}else
			{
				pItem->setFlags(Qt::ItemIsSelectable |
					Qt::ItemIsDragEnabled |
					Qt::ItemIsEnabled |
					Qt::ItemIsTristate);
			}
		}else
		{
			pItem->setFlags(Qt::ItemIsSelectable | 
				Qt::ItemIsEditable | 
				Qt::ItemIsDragEnabled |
				Qt::ItemIsDropEnabled |
				Qt::ItemIsUserCheckable |
				Qt::ItemIsEnabled |
				Qt::ItemIsTristate);
		}

		if (FieldNo==0)
		{
			pItem->setBackgroundColor(QColor(205, 205, 205));
		}
		return 1;
	}

	return 0;
}

//	设置记录的可编辑状态
int MyTableWidget::SetRowItemEditState(int RowNo, bool bEnableEdit)
{
	int columnNum = columnCount();
	QTableWidgetItem *pItem = NULL;
	for (int i=0; i<columnNum; i++)
	{
		pItem = item(RowNo, i);
		if (pItem)
		{
			if (bEnableEdit)
			{
				item(RowNo, i)->setFlags(Qt::ItemIsSelectable |
					Qt::ItemIsDragEnabled |
					Qt::ItemIsEnabled |
					Qt::ItemIsTristate |
					Qt::ItemIsDropEnabled |
					Qt::ItemIsUserCheckable |
					Qt::ItemIsEditable);
			}else
			{
				item(RowNo, i)->setFlags(Qt::ItemIsSelectable | 
					Qt::ItemIsDragEnabled |
					Qt::ItemIsEnabled |
					Qt::ItemIsTristate);
			}
		}

	}
	return 1;
}

// void MyTableWidget::mousePressEvent( QMouseEvent * event )
// {
//    if (event)
//    {
//         if (Qt::RightButton == event->button() &&
//             QEvent::MouseButtonPress == event->type())
//         {
//             QMenu   *pMenu = new QMenu(this);
//             pMenu->addMenu(QString::fromLocal8Bit("test"));
//             QPoint point = event->globalPos();
//             pMenu->popup(point);
// 
// 		}else if (Qt::LeftButton == event->button() &&
// 			QEvent::MouseButtonPress == event->type())
// 		{
// // 			QTableWidgetItem *pItem = NULL;
// // 			pItem = itemAt(event->pos());
// // 			if (pItem)
// // 			{
// // 				if (isItemSelected(pItem))
// // 				{
// // 					int CurFieldNo = column(pItem);
// // 					int CurRowNo = row(pItem);
// // 					int CurColumnNo = column(pItem);
// // 					QComboBox *pCombox = new QComboBox(this);
// // 					QStringList list;
// // 					list.append(QString::fromLocal8Bit("是"));
// // 					list.append(QString::fromLocal8Bit("否"));
// // 					pCombox->setDuplicatesEnabled(true);
// // 					pCombox->setMaxCount(2);
// // 					pCombox->addItems(list);
// // 					setCellWidget(CurRowNo, CurColumnNo,pCombox);
// // 				}
// // 			}
// 		}
//     }
// 
// 	QTableWidget::mousePressEvent(event);
// }

void MyTableWidget::closeEvent(QCloseEvent *event)
{
	if (!event)
	{
		return ;
	}

	if (m_pTableData->IsNewDataUnSaved()) 
	{
		QMessageBox Msg(QMessageBox::Question, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("有内容未保存, 是否保存？"));
		QAbstractButton  *pYesBtn = (QAbstractButton  *)Msg.addButton(QString::fromLocal8Bit("是"), QMessageBox::YesRole);
		QAbstractButton  *pNoBtn = (QAbstractButton  *)Msg.addButton(QString::fromLocal8Bit("否"), QMessageBox::NoRole);
		Msg.exec();
		if (Msg.clickedButton() == pYesBtn)
		{
			bSaveData();
		}
	}

	event->accept();
}

bool MyTableWidget::bSaveData()
{
	int NewPageNum = 0;
	//	int OldPageNum = m_pTableData->GetPageNum();
	int OldPageNo = m_pTableData->GetCurPageNo();
	// 	if (NewPageNum != OldPageNum)
	// 	{
	// 		emit PageNum_Signal(NewPageNum);
	// 		OnPageShow(PAGE_ACT_TYPE_MOVE_TAIL, 0);
	// 	}

	if (m_pTableData->bSaveData()<=0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("保存失败\r\n"));
		//	return false;
	}

	InitTableDataShow(true, OldPageNo);
	NewPageNum = m_pTableData->GetPageNum();
// 	if (m_CurTableId!=TABLE_RTASK_PARA_INFO)
// 	{
// 		emit UpTreeItem_Signal(m_CurAppType, m_CurTableId, m_CurParentId, 0, m_pCurTreeItem, OPER_ROW_INSERT);
// 	}

	return true;
}

void MyTableWidget::OnSynicUserDataChang(int RecordNo, int columnNo)
{
	CheckNeedSave(false);
	OneFieldDataShow(RecordNo%TABLE_PAGE_ROW_NUM, columnNo);
}

//	ITEM用户数据改变
void MyTableWidget::OnItemUserDataChang(int RecordNo, int columnNo)
{
	CheckNeedSave(false);
	emit Signal_UserDataChang(RecordNo, columnNo);
//	if (rowNo>=0 && columnNo>=0)
//	{
	//	QTableWidgetItem *pItem = NULL;
	//	pItem = item(rowNo, columnNo);
	//	if (pItem)
	//	{
	//		QVariant ItemData = pItem->data(Qt::UserRole);
	//		m_pTableData->UpDateDataChang(rowNo, columnNo, ItemData);
	//	}
//	}
}

//添加一条记录
void MyTableWidget::OnAppendRow()
{
	/*if (!m_pTableData->IsEnableAddNew())
	{
		return;
	}*/

	int OldCurPageNo = m_pTableData->GetCurPageNo();
	int NewPageNum = 0;
	int OldPageNum = m_pTableData->GetPageNum();
	m_pTableData->AddOneRecord();
	NewPageNum = m_pTableData->GetPageNum();
	int RowNo = rowCount();
	if (OldCurPageNo != NewPageNum-1)
	{
		OnPageShow(PAGE_ACT_TYPE_MOVE_TAIL, 0);
	}else
	{
		QTableWidget::insertRow(RowNo);
		OneRowTableDataShow(RowNo);
	}
	if (NewPageNum != OldPageNum)
	{
		emit PageNum_Signal(NewPageNum);
		OnPageShow(PAGE_ACT_TYPE_MOVE_TAIL, 0);
	}
	CheckNeedSave(false);
	QTableWidget::scrollToBottom();
}

//响应数据保存
void MyTableWidget::OnSaveData()
{
	bSaveData();

	//emit CheckOnline_Signal();
	CheckNeedSave(false);
}

//	数据删除标志
void MyTableWidget::OnDeleteData()
{
	int NewPageNum = 0;
	int OldPageNum = m_pTableData->GetPageNum();
	int OldCurPageNo = m_pTableData->GetCurPageNo();

	QTableWidgetItem * pVerticalItem = NULL;
	int nRowCout = rowCount();

	bool	bDelFlag = false;
	bool  bDelImmediately = false;
	bool	bAdjustRowNoFlag = false;
	int	RetValue = 0;
	QItemSelectionModel *pItemSelectModel = selectionModel();

	if (pItemSelectModel)
	{
		for (int i=0; i<nRowCout; i++)
		{
			bDelFlag = false; 
			pVerticalItem = verticalHeaderItem(i);
			if (pVerticalItem)
			{
				bDelFlag = pVerticalItem->data(Qt::UserRole).toInt();
				//bool temp1 = pItemSelectModel->rowIntersectsSelection(i,rootIndex());
				//if (pItemSelectModel->isRowSelected(i, rootIndex()))
				if(pItemSelectModel->rowIntersectsSelection(i,rootIndex()))//ccg17.5.3用rowIntersectsSelection替换函数isRowSelected
				{														   //不必选择行开头就能选中并删除。
					if (bDelFlag)
					{
						bDelFlag = false;
					}else
					{
						bDelFlag = true;
					}
					RetValue = m_pTableData->SetRecordDelete(i, bDelFlag, bDelImmediately);
					if (RetValue>0)
					{
						if (bDelImmediately)
						{
							removeRow(i);
							i -= 1;
							bAdjustRowNoFlag = true;
						}else
						{
							SetRowItemEditState(i,!bDelFlag);
							SetVerticalHeadShow(i, pVerticalItem, bDelFlag);
						}
					}
				}else
				{
					if (bAdjustRowNoFlag)
					{
						SetVerticalHeadShow(i, pVerticalItem, bDelFlag);
					}
				}
			}
		}
	}
	NewPageNum = m_pTableData->GetPageNum();
	if (NewPageNum != OldPageNum)
	{
		emit PageNum_Signal(NewPageNum);
		OnPageShow(PAGE_ACT_TYPE_MOVE_ASSIGN, OldCurPageNo);
	}
	emit SelectRow_Signal(1);
	CheckNeedSave(false);
}

//	数据刷新
void MyTableWidget::OnRefreshData()
{
	if (m_CurPtlParamType!=0)
	{
		for (int i=0;i<m_ParaList.count();i++)
		{
			item(i,1)->setData(Qt::DisplayRole,m_ParaList.at(i));
			item(i,1)->setData(Qt::UserRole,m_ParaList.at(i).toInt());
			item(i,1)->setData(Qt::TextColorRole,Qt::black);
		}
		CheckNeedSave(false);
	}
	else
	{
		InitTableDataShow(true, m_pTableData->GetCurPageNo());
		emit SelectRow_Signal(0);
		emit SelectItem_Signal(0, 0 ,0);
		CheckNeedSave(false);
	}
	RefrashTableData();
	emit ClickRow_Signal(-1);
	
}

//	重新设置数据显示 不删除ITEM
void MyTableWidget::OnReSetDataShow(int RecId1, int RecId2, int RecId3, int FieldNo)
{
	//	InitTableDataShow();
	//	return ;
	int OldPageNo = 0;
	OldPageNo = m_pTableData->GetCurPageNo();

	m_pTableData->ReleaseTableData();
	int rowNum = rowCount();

//	m_pTableData->InitTableData();
	if (!m_pTableData->IsRecordParamSafe())
	{
		return ;
	}
	int RecordNum = 0;
	int FieldNum = 0;
	int RowNum = 0;
	//RecordNum = m_pTableData->GetCurRecordNum();
	m_pTableData->SetCurPageNo(OldPageNo);
	RecordNum = m_pTableData->GetCurPageRecordNum();
	FieldNum = m_pTableData->GetCurShowFieldNum();
	RowNum = rowCount();
	if (FieldNum <= 0 || RecordNum<=0)
	{
		return ;
	}
	m_SetColorRecId1=RecId1;
	m_SetColorRecId2=RecId2;
	m_SetColorRecId3=RecId3;
	m_SetColorRecFieldNo=FieldNo;
	rowNum = (rowNum <= RecordNum ? rowNum:RecordNum);
	for (int i=0; i<rowNum; i++)
	{
		OneRowTableDataShow(i);
	}
	m_SetColorRecId1=0;
	m_SetColorRecId2=0;
	m_SetColorRecId3=0;
	m_SetColorRecFieldNo=0;
	return ;
}

//设置列值
void MyTableWidget::OnSetColValue()
{	
	int	li_BeginRow = 0;
	int	li_EndRow = 0;
	int	li_BeginCol = 0;
	int	li_EndCol = 0;
	int	li_FocusRow = 0;
	int	li_FocusCol = 0;	
	double ld_StartValue = 0;
	double ld_StepValue = 0;
	double ld_FocusValue = 0;
	bool  li_AddFlag = false;
	bool  li_SelectStartFlag = false;
	bool	li_bNumerFlag = true;
	short  li_DataType = 0;
	short  li_DisplayType = 0;
	QString  l_StrFocus;
	QVariant l_DataFocus;
	QString  l_StrStart;
	QVariant l_DataStart;
	QVariant li_DataFill;
	int     j = 0;
	bool   bColorField = false;
	bool	bSingleRelateCol = false;

	QList<QTableWidgetSelectionRange> list = selectedRanges();
	if (list.count()<=0)
	{
		return;
	}
	li_BeginRow = list[0].topRow();
	li_EndRow = list[0].bottomRow();
	li_BeginCol = list[0].leftColumn();
	li_EndCol = list[0].rightColumn();
	if (list.count()>1)
	{
		for (j=0; j<list.count(); j++)
		{
			if (list[j].topRow() < li_BeginRow)
			{
				li_BeginRow = list[j].topRow();
			}
			if (list[j].bottomRow() > li_EndRow)
			{
				li_EndRow = list[j].bottomRow();
			}
			if (list[j].leftColumn() < li_BeginCol)
			{
				li_BeginCol = list[j].rightColumn();
			}
			if (list[j].rightColumn() > li_EndCol)
			{
				li_EndCol = list[j].rightColumn();
			}
		}
	}

	if ((li_BeginCol != li_EndCol) || (li_BeginRow == li_EndRow))
	{
		QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("范围选择错误！"));
		return;
	}
	li_FocusRow = currentRow();
	li_FocusCol = currentColumn();
	bool bEdit = false;
	bool bEdit_once = false;
	unsigned int ShowFlag = 0;
	m_pTableData->IsCurFieldEnableEdit(li_BeginCol,bEdit, bEdit_once);
	if (!bEdit)
	{
		QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("该域不能手动输入！"));
		return;
	}
	
//	m_pTableData->GetUpDateFieldText(li_BeginRow, li_BeginCol, l_DataStart, l_StrStart, bColorField/*,-1*/);
//	m_pTableData->GetUpDateFieldText(li_FocusRow, li_FocusCol, l_DataFocus, l_StrFocus, bColorField/*,-1*/);
	m_pTableData->GetFieldText(li_BeginRow, li_BeginCol, l_DataStart, l_StrStart, bColorField,ShowFlag);
	m_pTableData->GetFieldText(li_FocusRow, li_FocusCol, l_DataFocus, l_StrFocus, bColorField,ShowFlag);
	li_DataType = m_pTableData->GetFieldDataType(li_BeginCol);
	li_DisplayType = m_pTableData->GetFieldDisplayType(li_BeginCol);
	bSingleRelateCol = m_pTableData->GetFieldIsSingleRelate(li_BeginCol);

	switch (li_DisplayType)
	{
	case DISPLAY_TYPE_CHAR:
	case DISPLAY_TYPE_UCHAR:
	case DISPLAY_TYPE_SHORT:
	case DISPLAY_TYPE_USHORT:
	case DISPLAY_TYPE_INT32:
	case DISPLAY_TYPE_UINT32:
	case DISPLAY_TYPE_LONGLONG:
	case DISPLAY_TYPE_ULONGLONG:
	case DISPLAY_TYPE_FLOAT:
	case DISPLAY_TYPE_DOUBLE:
		{
			double tempvalue = l_DataStart.toDouble();
			ld_StartValue = tempvalue;
			tempvalue = 0;
			tempvalue = l_DataFocus.toDouble();
			ld_FocusValue = tempvalue;
			li_bNumerFlag = true;
		}
		break;
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_BOOLEN:
	case DISPLAY_TYPE_MENU:
	case DISPLAY_TYPE_MULTI_MENU:
	case DISPLAY_TYPE_DATE:
	case DISPLAY_TYPE_TIME:
	case DISPLAY_TYPE_DATETIME:
	case DISPLAY_TYPE_TOTAL_TIME:
	case DIPPLAY_TYPE_COLOR:
	case DISPLAY_TYPE_PASSWORD:
	case DISPLAY_TYPE_HEX:
	case DISPLAY_TYPE_IP_CFG:	
		{
			li_bNumerFlag = false;
		}
		break;
	case DISPLAY_TYPE_RELATED:
		{
			if (bSingleRelateCol)
			{
				li_bNumerFlag = false;
			}else
			{
				QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("该字段为对称关联字段，不允许按列设值！"));		
				return ;
			}
		}
		break;
	case DISPLAY_TYPE_TABLE:
	case DISPLAY_TYPE_COLUMN:
	case DISPLAY_TYPE_EXP:
	case DISPLAY_TYPE_USER_DEFINE:
	case DISPLAY_TYPE_INDEXNO:
//	case DISPLAY_TYPE_NODE_CFG_STATE:
		{
			QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("该域不允许按列设值！"));		
			return ;
		}
		break;
	default:
		break;
	}

	SetColValDlg Dlg(li_BeginRow, li_EndRow, l_StrStart, ld_StartValue, l_StrFocus, ld_FocusValue, li_bNumerFlag);
	if (QDialog::Accepted != Dlg.exec())
	{
		return;
	}
	Dlg.GetParam(li_AddFlag, ld_StartValue, ld_StepValue, li_SelectStartFlag);	
	for (int i=li_BeginRow; i<=li_EndRow; i++)
	{
		if (!item(i, li_BeginCol)->isSelected())
		{
			continue;
		}else
		{
			if (li_bNumerFlag)
			{
				if (i != li_BeginRow)
				{
					if (li_AddFlag)
					{
						ld_StartValue += ld_StepValue;
					}else
					{
						ld_StartValue *= ld_StepValue;
					}
				}

				switch (li_DisplayType)
				{
				case DISPLAY_TYPE_FLOAT:
					{
						float  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
				case DISPLAY_TYPE_DOUBLE:
					{
						double  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}						
					break;
				case DISPLAY_TYPE_CHAR:
					{
						char  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}						
					break;
				case DISPLAY_TYPE_UCHAR:
					{
						unsigned char  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				case DISPLAY_TYPE_SHORT:
					{
						short  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				case DISPLAY_TYPE_USHORT:
					{
						unsigned short  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				case DISPLAY_TYPE_INT32:
					{
						int  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				case DISPLAY_TYPE_UINT32:
					{
						unsigned int  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				case DISPLAY_TYPE_LONGLONG:
					{
						qint64  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				case DISPLAY_TYPE_ULONGLONG:
					{
						quint64  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				default:
					{
						int  tempValue = 0;
						tempValue = ld_StartValue;
						li_DataFill = QVariant(tempValue);
					}
					break;
				}
			}else
			{
				if (li_SelectStartFlag)
				{
					li_DataFill = l_DataStart;
				}else
				{
					li_DataFill = l_DataFocus;
				}
			}
			m_pTableData->UpDateDataChang(i, li_BeginCol, li_DataFill);
			OneFieldDataShow(i, li_BeginCol);
		}
	}
	for (j=0; j<list.count(); j++)
	{
		setRangeSelected(list[j], false); 
	}
	CheckNeedSave(false);
}

//	判断行选中
void MyTableWidget::OnClickVHead(int RowNo)
{
	if (RowNo>=0 && RowNo<rowCount())
	{
		int RecordNo = m_pTableData->GetCurRecordNoByRowNo(RowNo);
		emit ClickRow_Signal(RecordNo);
	}
}

void MyTableWidget::OnItemClick(QTableWidgetItem *item)
{
	m_CurSeltRowNo = -1;
	m_CurSeltColumnNo = -1;
	if (!item)
	{
		return ;
	}

	if (item)
	{
		m_CurSeltRowNo = item->row();
		m_CurSeltColumnNo = item->column();
	}
	
	if (m_CurSeltRowNo>=0 && m_CurSeltRowNo<rowCount())
	{
		OnClickVHead(m_CurSeltRowNo);
	}
}

//	判断行选中
void MyTableWidget::OnHScrollMove(int moveValue)
{
	// 	if ( !isColumnHidden(1))
	// 	{
	// 		setColumnHidden(1, true);
	// 	}else
	// 	{
	// 		setColumnHidden(1, false);
	// 	}
	// 	QRect ClientRect;
	// 	ClientRect = rect();
	// 	ClientRect.setLeft(ClientRect.left()+columnWidth(0));
	// 	ClientRect.setRight(ClientRect.right()-columnWidth(0));
	// 
	// 	repaint(ClientRect);

	//	qDebug() << "width="<< columnWidth(0) <<moveValue;

}

//选择内容改变
void MyTableWidget::SelectChanged()
{
	int nRowCout = rowCount();
	int nColCout = columnCount();
	int RowNum = 0;
	int ItemRowNum = 0;
	int ItemColNum = 0;
	bool  bNewSetColFlag = false;
	QItemSelectionModel *pItemSelectModel = selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<nRowCout; i++)
		{
		    //if (pItemSelectModel->isRowSelected(i,rootIndex())) //ccg:isRowSelected：如果该行被全选，则返回true;
			//bool temp1 = pItemSelectModel->rowIntersectsSelection(i,rootIndex());
			if(pItemSelectModel->rowIntersectsSelection(i,rootIndex())) //ccg17.5.2:Returns true if there are any items selected in the row with the given parent.
			{
				RowNum++;
			}
		}
	}

	QList<QTableWidgetSelectionRange> list = selectedRanges();
	if (list.count()>0)
	{
		int li_BeginRow = list[0].topRow();
		int li_EndRow = list[0].bottomRow();
		int li_BeginCol = list[0].leftColumn();
		int li_EndCol = list[0].rightColumn();
		int j=0;
		if (list.count()>1)
		{
			for (j=0; j<list.count(); j++)
			{
				if (list[j].topRow() < li_BeginRow)
				{
					li_BeginRow = list[j].topRow();
				}
				if (list[j].bottomRow() > li_EndRow)
				{
					li_EndRow = list[j].bottomRow();
				}
				if (list[j].leftColumn() < li_BeginCol)
				{
					li_BeginCol = list[j].rightColumn();
				}
				if (list[j].rightColumn() > li_EndCol)
				{
					li_EndCol = list[j].rightColumn();
				}
			}
		}

		ItemRowNum = li_EndRow-li_BeginRow+1;
		ItemColNum = li_EndCol-li_BeginCol+1;
	}

	if (/*m_LastSelRowNum != RowNum*/RowNum>0) 
	{
		m_LastSelRowNum = RowNum;
	}else
	{
		m_LastSelRowNum=0;
	}
	if (IsCurTableEnableEdit(m_CurAppType, m_CurTableId, m_CurParentId)/* &&
		(TABLE_RTRANSMIT_YX != m_CurTableId && 
		TABLE_RTRANSMIT_YC != m_CurTableId &&
		TABLE_RTRANSMIT_YK != m_CurTableId &&
		TABLE_RTRANSMIT_YM != m_CurTableId ) */)
	{
		emit SelectRow_Signal(m_LastSelRowNum);
	}

	if (ItemRowNum>1 && ItemColNum==1)
	{
		bNewSetColFlag = true;
	}

	if (bNewSetColFlag != m_LastbSetColFlag)
	{	
		m_LastSelItemRowNum = ItemRowNum;
		m_LastSelItemColNum = ItemColNum;
		m_LastbSetColFlag = bNewSetColFlag;
		if (IsCurTableEnableEdit(m_CurAppType, m_CurTableId, m_CurParentId)/* &&
			(TABLE_RTRANSMIT_YX != m_CurTableId && 
			TABLE_RTRANSMIT_YC != m_CurTableId &&
			TABLE_RTRANSMIT_YK != m_CurTableId &&
			TABLE_RTRANSMIT_YM != m_CurTableId ) */)
		{
			if (m_LastSelItemColNum==1&&m_LastSelItemRowNum>1)
			{
				for (int i=0; i<nColCout; i++)
				{
					if (pItemSelectModel->isColumnSelected(i, rootIndex()))
					{
						if (this->horizontalHeaderItem(i)->text()==QString::fromLocal8Bit("规约"))
						{
							emit SelectItem_Signal(m_LastSelItemRowNum, m_LastSelItemColNum,1);	
							return;
						}
					}
				}
			}
			emit SelectItem_Signal(m_LastSelItemRowNum, m_LastSelItemColNum,0);	
		}
	}
	else
	{
		if (IsCurTableEnableEdit(m_CurAppType, m_CurTableId, m_CurParentId)/* &&
																		   (TABLE_RTRANSMIT_YX != m_CurTableId && 
																		   TABLE_RTRANSMIT_YC != m_CurTableId &&
																		   TABLE_RTRANSMIT_YK != m_CurTableId &&
																		   TABLE_RTRANSMIT_YM != m_CurTableId ) */)
		{
			if (ItemColNum==1&&ItemRowNum>1)
			{
				for (int i=0; i<nColCout; i++)
				{
					if (pItemSelectModel->isColumnSelected(i, rootIndex()))
					{
						if (this->horizontalHeaderItem(i)->text()==QString::fromLocal8Bit("规约"))
						{
							emit SelectItem_Signal(ItemRowNum, ItemColNum,1);	
							return;
						}
					}
				}
			}
			emit SelectItem_Signal(ItemRowNum, ItemColNum,0);	
		}
	}
}


//	将设备保存为模板
/*void MyTableWidget::SaveDeviceToModel()
{
	int	seltRowNo = -1;
	//	QTableWidgetItem * pVerticalItem = NULL;
	int nRowCout = rowCount();
	QItemSelectionModel *pItemSelectModel = selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<nRowCout; i++)
		{
			if (pItemSelectModel->isRowSelected(i, rootIndex()))
			{
				seltRowNo = i;
				break;
			}
		}
	}
	if (seltRowNo>=0)
	{
		unsigned int	CurRecordId = 0;
		m_pTableData->GetCurRecordIdbyRowIndex(seltRowNo, CurRecordId);
		if (CurRecordId<=0)
		{
			return;
		}
		SaveDevToModelDlg  Dlg(m_CurAppType, m_CurTableId, CurRecordId);
		if (m_pTreeWdgt)
		{
			connect(&Dlg, SIGNAL(UpDateTreeItem_Signal(quint32)), m_pTreeWdgt, SLOT(OnAddTreeDevModelItem(quint32)));
		}

		Dlg.exec();
	}
}*/

//	将设备模板保存为文件
/*void MyTableWidget::SaveDevModelToFile()
{
	int	seltRowNo = -1;
	//	QTableWidgetItem * pVerticalItem = NULL;
	int nRowCout = rowCount();
	QItemSelectionModel *pItemSelectModel = selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<nRowCout; i++)
		{
			if (pItemSelectModel->isRowSelected(i, rootIndex()))
			{
				seltRowNo = i;
				break;
			}
		}
	}
	if (seltRowNo>=0)
	{
		unsigned int	CurRecordId = 0;
		m_pTableData->GetCurRecordIdbyRowIndex(seltRowNo, CurRecordId);
		if (CurRecordId<=0)
		{
			return;
		}
		SaveModelToFile  Dlg(m_CurAppType, m_CurTableId, CurRecordId);
		Dlg.exec();
	}
}*/

void MyTableWidget::CheckNeedSave(bool isPara)
{
	if (isPara)
	{
		emit SaveData_Signal(true);
	}
	else
	{
		if (m_pTableData->IsNewDataUnSaved())
		{
			emit SaveData_Signal(true);
		}else
		{
			emit SaveData_Signal(false);
		}
	}
}

//
void MyTableWidget::OnPageShow(int ActType, int PageNo)
{								  
/*	//	bool bReshow = false;
	switch (ActType)
	{
	case PAGE_ACT_TYPE_MOVE_HEAD:
		{
			m_pTableData->SetCurPageNo(0);
			ShowTableData(false,0);
		}
		break;
	case PAGE_ACT_TYPE_MOVE_PREV:
		{
			m_pTableData->SetPrePage();
			ShowTableData(false,0);
		}
		break;
	case PAGE_ACT_TYPE_MOVE_NEXT:
		{
			m_pTableData->SetNextPage();
			ShowTableData(false,0);
		}
		break;
	case PAGE_ACT_TYPE_MOVE_TAIL:
		{
			int PageNum = m_pTableData->GetPageNum();
			if (PageNum>0)
			{
				m_pTableData->SetCurPageNo(PageNum-1);
			}else
			{
				m_pTableData->SetCurPageNo(0);
			}

			ShowTableData(false,0);
		}
		break;
	case PAGE_ACT_TYPE_MOVE_ASSIGN:
		{
			m_pTableData->SetCurPageNo(PageNo);
			ShowTableData(false,0);
		}
		break;
	default:
		break;
	}
*/
}

bool MyTableWidget::CheckIsSetChangColorRecord(int RecordId)
{
	if (m_SetColorRecId1>0 && RecordId == m_SetColorRecId1 )
	{
		m_SetColorRecId1 = 0;
		return true;
	}
	if (m_SetColorRecId2>0 && RecordId == m_SetColorRecId2)
	{
		m_SetColorRecId2 = 0;
		return true;
	}
	if (m_SetColorRecId3>0 && RecordId == m_SetColorRecId3)
	{
		m_SetColorRecId3 = 0;
		return true;
	}

	return false;
}

//	从剪切板取数据
void MyTableWidget::OnGetDataFromCliboard()
{
	int	li_BeginRow = 0;
	int	li_EndRow = 0;
	int	li_BeginCol = 0;
	int	li_EndCol = 0;
	//	int	li_FocusRow = 0;
	//	int	li_FocusCol = 0;	
	//	double ld_StartValue = 0;
	//	double ld_StepValue = 0;
	//	double ld_FocusValue = 0;
	//	bool  li_AddFlag = false;
	//	bool  li_SelectStartFlag = false;
	//	bool	li_bNumerFlag = true;
	//	short  li_DataType = 0;
	short  li_DisplayType = 0;
	//	QString  l_StrFocus;
	//	QVariant l_DataFocus;
	//	QString  l_StrStart;
	//	QVariant l_DataStart;
	QVariant li_DataFill;
	int     j = 0;
	//	bool   bColorField = false;
	//	bool	bSingleRelateCol = false;

	QList<QTableWidgetSelectionRange> list = selectedRanges();
	if (list.count()<=0)
	{
		return;
	}
	li_BeginRow = list[0].topRow();
	li_EndRow = list[0].bottomRow();
	li_BeginCol = list[0].leftColumn();
	li_EndCol = list[0].rightColumn();
	if (list.count()>1)
	{
		for (j=0; j<list.count(); j++)
		{
			if (list[j].topRow() < li_BeginRow)
			{
				li_BeginRow = list[j].topRow();
			}
			if (list[j].bottomRow() > li_EndRow)
			{
				li_EndRow = list[j].bottomRow();
			}
			if (list[j].leftColumn() < li_BeginCol)
			{
				li_BeginCol = list[j].rightColumn();
			}
			if (list[j].rightColumn() > li_EndCol)
			{
				li_EndCol = list[j].rightColumn();
			}
		}
	}

	if ((li_BeginCol != li_EndCol))
	{
		QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("选择了多列，无法粘贴！"));
		return;
	}

	//if (!m_pTableData->IsCurFieldEnableEdit(li_BeginCol))
	bool bEdit = false;
	bool bEdit_once = false;
	m_pTableData->IsCurFieldEnableEdit(li_BeginCol,bEdit, bEdit_once);
	if (!bEdit)
	{
		QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("该域不能手动输入！"));
		return;
	}

	//	m_pTableData->GetUpDateFieldText(li_BeginRow, li_BeginCol, l_DataStart, l_StrStart, bColorField);
	//m_pTableData->GetUpDateFieldText(li_FocusRow, li_FocusCol, l_DataFocus, l_StrFocus, bColorField);
	//	li_DataType = m_pTableData->GetFieldDataType(li_BeginCol);
	li_DisplayType = m_pTableData->GetFieldDisplayType(li_BeginCol);

	switch (li_DisplayType)
	{
	case DISPLAY_TYPE_RELATED:
		{
			QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("该字段为对称关联字段，不允许按列设值！"));		
			return ;
		}
		break;
	case DISPLAY_TYPE_TABLE:
	case DISPLAY_TYPE_COLUMN:
	case DISPLAY_TYPE_EXP:
	case DISPLAY_TYPE_USER_DEFINE:
	case DISPLAY_TYPE_INDEXNO:
//	case DISPLAY_TYPE_NODE_CFG_STATE:
		{
			QMessageBox::question(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("该域不允许按列设值！"));		
			return ;
		}
		break;
	default:
		break;
	}

	QClipboard *pCliboard = NULL;
	const QMimeData  *pMimeData = NULL;
	QString str;
	pCliboard = QApplication::clipboard();

	if (pCliboard)
	{
		pMimeData = pCliboard->mimeData();
	}
	if (!pMimeData)
	{
		return ;
	}

	QStringList strlist;

	if (pMimeData->hasText())
	{
		str = pMimeData->text();
		strlist = str.split("\n");
	}
	QString Str2;
	int lstcount= strlist.count();
	int RowNo = 0;
	RowNo = li_BeginRow;
	for (int k=0; k<lstcount; k++)
	{
		Str2.clear();
		Str2 = strlist.at(k);
		Str2 = Str2.trimmed();
		if (Str2.length()<=0)
		{
			continue;
		}
		switch (li_DisplayType)
		{
		case DISPLAY_TYPE_FLOAT:
			{
				float  tempValue = 0;
				tempValue = Str2.toFloat();
				li_DataFill = QVariant(tempValue);
			}
		case DISPLAY_TYPE_DOUBLE:
			{
				double  tempValue = 0;
				tempValue = Str2.toDouble();
				li_DataFill = QVariant(tempValue);
			}						
			break;
		case DISPLAY_TYPE_CHAR:
			{
				char  tempValue = 0;
				tempValue = Str2.toInt();
				li_DataFill = QVariant(tempValue);
			}						
			break;
		case DISPLAY_TYPE_UCHAR:
			{
				unsigned char  tempValue = 0;
				tempValue = Str2.toUInt();
				li_DataFill = QVariant(tempValue);
			}
			break;
		case DISPLAY_TYPE_SHORT:
			{
				short  tempValue = 0;
				tempValue = Str2.toInt();
				li_DataFill = QVariant(tempValue);
			}
			break;
		case DISPLAY_TYPE_USHORT:
			{
				unsigned short  tempValue = 0;
				tempValue = Str2.toUInt();
				li_DataFill = QVariant(tempValue);
			}
			break;
		case DISPLAY_TYPE_INT32:
		case DISPLAY_TYPE_BOOLEN:
			{
				int  tempValue = 0;
				tempValue = Str2.toInt();
				li_DataFill = QVariant(tempValue);
			}
			break;
		case DISPLAY_TYPE_UINT32:
			{
				unsigned int  tempValue = 0;
				tempValue = Str2.toUInt();
				li_DataFill = QVariant(tempValue);
			}
			break;
		case DISPLAY_TYPE_LONGLONG:
			{
				qint64  tempValue = 0;
				tempValue = Str2.toLongLong();
				li_DataFill = QVariant(tempValue);
			}
			break;
		case DISPLAY_TYPE_ULONGLONG:
			{
				quint64  tempValue = 0;
				tempValue = Str2.toULongLong();
				li_DataFill = QVariant(tempValue);
			}
			break;
		case DISPLAY_TYPE_STRING:
			{
				li_DataFill = QVariant(Str2);
			}
			break;
		default:
			{
				unsigned int  tempValue = 0;
				tempValue = Str2.toUInt();
				li_DataFill = QVariant(tempValue);
			}
			break;
		}

		m_pTableData->UpDateDataChang(RowNo, li_BeginCol, li_DataFill);
		OneFieldDataShow(RowNo, li_BeginCol);
		RowNo++;
		if (RowNo>=TABLE_PAGE_ROW_NUM)
		{
			break;
		}
	}

	for (j=0; j<list.count(); j++)
	{
		setRangeSelected(list[j], false); 
	}
	CheckNeedSave(false);
}

//建立右键菜单
void MyTableWidget::setupPopMenu()
{
	m_RighMenu=new QMenu("PopMenu",this);
	QAction *action1 = m_RighMenu->addAction(QString::fromLocal8Bit("粘贴"));
	connect(action1, SIGNAL(triggered()), this, SLOT(OnGetDataFromCliboard()));
	action1->setData(VIEW_MENU_PASTE);
}

void MyTableWidget::upDateMenu(QPoint point)
{
	if(m_RighMenu==NULL)
	{
		return;
	}
	m_RighMenu->popup(point);
	int index =0;
	int Menu_Count =0;
	int i=0;
	QAction * p_action = NULL;
	int data_menu=0;
	bool ok =false;
	bool	menuenable=false;
	index=0;
	Menu_Count=0;

	m_PopAction_list =  m_RighMenu->actions();
	Menu_Count=m_PopAction_list.size();
	if(Menu_Count<=0)
	{
		return;
	}
	for(i=0;i<Menu_Count;i++)
	{
		p_action=m_PopAction_list.at(i);
		data_menu = p_action->data().toInt(&ok);
		if(ok == false)continue;
		switch(data_menu)
		{
		case VIEW_MENU_PASTE:
			{
				menuenable = false;
				QClipboard *pCliboard = NULL;
				const QMimeData  *pMimeData = NULL;
				pCliboard = QApplication::clipboard();

				if (pCliboard)
				{
					pMimeData = pCliboard->mimeData();
				}
				if (pMimeData)
				{
					if (pMimeData->hasText())
					{
						menuenable = true;
					}
				}
			}
			break;

		default:
			menuenable = false;
			break;
		}
		p_action->setEnabled(menuenable);
	}
}


void MyTableWidget::mousePressEvent( QMouseEvent * event )
{
	if (event)
	{
		if (Qt::RightButton == event->button() &&
			QEvent::MouseButtonPress == event->type())
		{
			QPoint point = event->globalPos();
			if (m_LastSelItemRowNum>1 && m_LastSelItemColNum==1)
			{
				upDateMenu(point);
				//emit PopMenu_Signal(point);
			}
		}
	} 
	QTableWidget::mousePressEvent(event);
}

//	将设备保存为模板
void MyTableWidget::SaveDeviceToModel()
{
// 	if (m_CurTableId<=0 && m_CurAppType<=0)
// 	{
// 		SaveTerminalToModelDlg dlg;
// 		if (m_pTreeWdgt)
// 		{
// 			connect(&dlg, SIGNAL(UpDateTreeItem_Signal(quint32)), m_pTreeWdgt, SLOT(OnAddTreeDevModelItem(quint32)));
// 		}
// 		dlg.exec();
// 	}else
// 	{
		int	seltRowNo = -1;
		//	QTableWidgetItem * pVerticalItem = NULL;
		int nRowCout = rowCount();
		QItemSelectionModel *pItemSelectModel = selectionModel();
		if (pItemSelectModel)
		{
			for (int i=0; i<nRowCout; i++)
			{
				if (pItemSelectModel->isRowSelected(i, rootIndex()))
				{
					seltRowNo = i;
					break;
				}
			}
		}
		if (seltRowNo>=0)
		{
			unsigned int	CurRecordId = 0;
			m_pTableData->GetCurRecordIdbyRowIndex(seltRowNo, CurRecordId);
			if (CurRecordId<=0)
			{
				return;
			}
			SaveDevToModelDlg  Dlg(m_CurAppType, m_CurTableId, CurRecordId);
			if (m_pTreeWdgt)
			{
				connect(&Dlg, SIGNAL(UpDateTreeItem_Signal(quint32)), m_pTreeWdgt, SLOT(OnAddTreeDevModelItem(quint32)));
			}

			Dlg.exec();
		}
	//}
}

//	将设备模板保存为文件
void MyTableWidget::SaveDevModelToFile()
{
	int	seltRowNo = -1;
	//	QTableWidgetItem * pVerticalItem = NULL;
	int nRowCout = rowCount();
	QItemSelectionModel *pItemSelectModel = selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<nRowCout; i++)
		{
			if (pItemSelectModel->isRowSelected(i, rootIndex()))
			{
				seltRowNo = i;
				break;
			}
		}
	}
	if (seltRowNo>=0)
	{
		unsigned int	CurRecordId = 0;
		m_pTableData->GetCurRecordIdbyRowIndex(seltRowNo, CurRecordId);
		if (CurRecordId<=0)
		{
			return;
		}
		SaveDevToModelFile  Dlg(m_CurAppType, m_CurTableId, CurRecordId);
		Dlg.exec();
	}
}